<?php

/**
 * 文章分类操作
 * @author pirate
 */
class Edm_Db_ArticleCat extends Zend_Db_Table {

    protected $_name = "article_cat";
    protected $_primary = 'cat_id';
    protected $_db;

    function init() {
        $this->_db = $this->getAdapter();
        parent::init();
    }

    /*
     * 分类个数
     * @return int 分类个数
     */

    public function catCount() {
        return $this->fetchAll()->count();
    }

    /*
     * 检查分类名称是否存在
     * @param array $cat 传入参数
     * @return bool 
     */

    public function catExists($cat) {
        $where = null;
        if (isset($cat['parentid'])) {
            $where = $this->_db->quoteInto(' parentid = ?', $cat['parentid']);
        }
        if (isset($cat['cat_name'])) {
            $str = $where !== null ? ' AND cat_name = ?' : ' cat_name = ?';
            $where .= $this->_db->quoteInto($str, $cat['cat_name']);
        }
        /* 如果是修改 */
        if (isset($cat['act'])) {
            $where .= $this->_db->quoteInto(' AND cat_id != ?', $cat['cat_id']);
        }
        $sql = "SELECT COUNT(*) FROM " . $this->_name . " WHERE" . $where;
        $result = $this->_db->fetchOne($sql);
        return $result;
    }

    /*
     * 插入文章分类
     * @param array $cat 分类参数
     * @return bool
     */

    public function catInsert($cat) {
        return $this->_db->insert($this->_name, $cat);
    }

    /*
     * 更新文章分类
     * @param array $cat 分类参数
     * @return bool
     */

    public function catUpdate($data, $cat_id) {
        $where = $this->_db->quoteInto('cat_id = ?', $cat_id);
        $res = $this->_db->update($this->_name, $data, $where);
        return $res;
    }

    /*
     * 删除文章分类
     * @param array $cat 分类参数
     * @return bool
     */

    public function catDelete($cat_id) {
        $where = $this->_db->quoteInto('cat_id = ?', $cat_id);
        $res = $this->_db->delete($this->_name, $where);
        return $res;
    }

    /*
     * 根据某个cat_id 获取信息
     * @param int $cat 分类参数
     * @return rows
     */

    public function getCatInfo($cat_id = 0) {
        $sql = "SELECT * FROM " . $this->_name . " WHERE cat_id =:cat_id LIMIT 1";
        $res = $this->_db->fetchRow($sql, array('cat_id' => $cat_id));
        return $res;
    }

    /*
     * 获取文章分类列表
     * @param int $cat_id  分类编号
     * @param int $level 层次数 0 为不控制
     * @param array $cols 欲取的字段
     * @return array
     */

    public function getArticleCat($parentid = 0, $level = 0, $cols = array('*')) {
        static $catRes;
        static $catLevel = 0;
        if ($parentid == 0 && $level != 0) {
            $catLevel = $level;
        }
        if ($catLevel > 0 && $level > $catLevel) {
            return $catRes;
        }
        $sql = "SELECT " . join(', ', $cols) . " FROM " . $this->_name . " WHERE";
        $sql .= $this->_db->quoteInto(' parentid = ?', $parentid);
        $rows = $this->_db->fetchAll($sql);
        if (count($rows)) {
            foreach ($rows as $item) {
                $item['level'] = $level;
                $catRes[] = $item;
                $this->getArticleCat($item['cat_id'], $level + 1, $cols);
            }
        }

        return $catRes;
    }

}
